package cn.tom.basic.util;

import com.alibaba.fastjson.JSONObject;
//minio下
import okhttp3.*;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * 工具类：百度AI智能审核
 */
public class BaiduAiUtils {
    static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();

    /**
     * 获取token
     * @return
     * @throws IOException
     */
    public static String getToken() throws IOException {
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, "");
        Request request = new Request.Builder()
                .url("https://aip.baidubce.com/oauth/2.0/token?client_id=PWTmDs8oPpwLBVIrCTTuGgVI&client_secret=odGDrBwUKK6OtnZl7bQepDGTXqEuSazO&grant_type=client_credentials")
                .method("POST", body)
                .addHeader("Content-Type", "application/json")
                .addHeader("Accept", "application/json")
                .build();
        Response response = HTTP_CLIENT.newCall(request).execute();
        String jsonStr = response.body().string();
        //处理json数据
        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        return jsonObject.getString("access_token");
    }


    /**
     * 文本审核
     * @param content
     * @return
     */
    public static Boolean textCensor(String content) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined";
        try {
            String param = "text=" + URLEncoder.encode(content, "utf-8");
            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = getToken();
            //发请求去审核文本
            String result = HttpUtil.post(url, accessToken, param);
            JSONObject jsonObject = JSONObject.parseObject(result);
            String conclusion = jsonObject.getString("conclusion");
            if (conclusion.equals("合规")) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


    /**
     * 图片审核
     * @param imgUrl
     * @return
     */
    public static Boolean imageCensor(String imgUrl) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/solution/v1/img_censor/v2/user_defined";
        try {
            // 本地文件路径
            String imgParam = URLEncoder.encode(imgUrl, "UTF-8");
            String param = "imgUrl=" + imgParam;
            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = getToken();
            String result = HttpUtil.post(url, accessToken, param);
            JSONObject jsonObject = JSONObject.parseObject(result);
            String conclusion = jsonObject.getString("conclusion");
            if (conclusion.equals("合规")) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 如果imgUrls=null或"" - 不审核
     * 如果imgUrls只有一张图片 - ？
     * 多张图片如何审核 - ？【多张图片格式用,拼接【地址是全地址】】
     * http:123.23.232.1:8099/xx.png,http:123.23.232.1:8099/yy.png,http:123.23.232.1:8099/zz.png
     * @param imgUrls
     * @return
     */
    public static Boolean imagesCensor(String imgUrls){
        if(imgUrls==null || imgUrls.trim().equals("")){
            return true;
        }

        String[] urlArr = imgUrls.split(",");
        for (String url : urlArr) {
            if(!imageCensor(url)){
                return false;
            }
        }

        return true;
    }

}
